function keyDownSearch(e) {
    var keyCode = e.keyCode;
    if (keyCode == 13) {
        global_Art_Var.pageNum = 1;//重置分页
        loadMessageList(true);
        return false;
    }
}

function tableFilterResetClick() {
    var $input = $(".message-panel .panel-body form input");
    $input.removeAttr("data");
    $("#menu").removeData("id");
    $input.val("");
    global_Art_Var.pageNum = 1;//重置分页
    loadMessageList(true);
}

function closeMessageWin() {
    $(".message-win").fadeOut();
}

function winFooterBtnClick() {
    var commnetId = $(this).parents(".message-win").attr("comm_id");
    if ($(this).hasClass("audit")) {
        var param = {};
        param.commentId = $(this).parents(".message-win").attr("comm_id");
        param.status = $(this).attr("data-status");//审核状态
        param.isSendEmail = 0;//不发送邮件
        auditMessage(param);
    } else {
        layerOpen(commnetId);
    }
}

function optsBtnClick() {
    var commentId = $(this).parents("tr").attr("data-id");
    if ($(this).hasClass("opt-reply")) {
        layerOpen(commentId);
    } else if ($(this).hasClass("opt-detail")) {
        var $con = $(".message-win .con");
        var $tr = $(this).parents("tr");
        $con.find("h4").text($tr.find(".art_title").text());
        $con.find(".person span").text($tr.find(".comm_user").text());
        $con.find(".email span").text($tr.find(".email").text());
        $con.find(".ip span").text($tr.find(".ip").text());
        $con.find(".notice-content").html($tr.data("content"));
        $con.parents(".message-win").attr("comm_id", $tr.attr("data-id"));
        $(".message-win").fadeIn();
    }
}

function layerFooterBtnClick() {
    if (!$(".message-win-reply .reply-content").text().trim()) {
        layer.msg("请输入内容", fn);
        return false;
    }
    var param = {};
    param.commentId = $(this).parent().attr("commentId");
    param.status = $(this).attr("data-status");//审核状态
    param.isSendEmail = $(this).attr("data-email");//是否发送邮件
    param.content = $(".message-win-reply .reply-content").html();
    auditMessage(param);
    return false;
}

function layerOpen(commentId) {
    var W = 400;

    layer.open({
        type: 1,
        skin: 'layui-layer-rim', //加上边框
        area: [W + "px", '250px'], //宽高
        offset: [150, $(".content-wrapper").width() / 2 + $(".content-wrapper").offset().left - W / 2],
        content: "<div class='message-win-reply'>" +
            "    <div class='reply-content form-control' contenteditable='true' ></div>" +
            "     <div class='reply-footer' commentId='" + commentId + "'>" +
            "        <a class='btn  btn-warning audit-pass-reply pull-left' data-status='1' data-email='0'>通过</a>" +
            "        <a class='btn  btn-warning audit-pass-reply pull-left' data-status='-1' data-email='0'>不通过</a>" +
            "        <a class='btn  btn-danger audit-pass-reply pull-left' data-status='1' data-email='1'>通过并发送邮件</a>" +
            "        <a class='btn  btn-danger audit-pass-reply pull-left' data-status='-1' data-email='1'>不通过并发送邮件</a>" +
            "     </div>" +
            "</div>"
    });
}

function auditMessage(param) {
    var shade = layer.load(0, {
        shade: [0.5, '#000'] //0.1透明度的黑色背景
    });
    $.ajax({
        url: "/message/back/auditMessage.zb",
        type: "POST",
        dataType: "JSON",
        data: param
    }).done(function (res) {
        if (res && res.success) {
            if ($("#message-table tbody tr").length <= 1) {
                global_Art_Var.isLoadPagination = true;
            }
            var $small = $(".sidebar-menu .comment small")
            $small.text(($small.text() - 0) - 1);
            if ($(".message-win").is(":visible")) {
                $(".message-win").fadeOut();
            }
            loadMessageList();
            layer.closeAll();
            layer.msg(res.msg);
        } else {
            layer.msg(res.msg || "审核失败！", fn);
        }
    }).fail(function () {
        layer.msg("可能由于网路等原因，审核失败！", fn);
    }).always(function () {
        layer.close(shade);
    })
}

function loadMessageList(isLoadPagination) {
    var param = {};
    param.pageNum = global_Art_Var.pageNum;
    param.pageSize = global_Art_Var.pageSize;
    param.title = $("#title").val().trim();
    param.auditStatus = $("#auditStatus").attr("data");
    param.commStartTime = $("#commStartTime").val().trim();
    param.commEndTime = $("#commEndTime").val().trim();
    param.auditStartTime = $("#auditStartTime").val().trim();
    param.auditEndTime = $("#auditEndTime").val().trim();
    param.blog_navbar_id = $("#menu").data("id")
    $.ajax({
        url: "/message/back/getMessage.zb",
        type: "POST",
        dataType: "JSON",
        contentType: "application/json",
        data: JSON.stringify(param)
    }).done(function (data) {
        var $tbody = $("#message-table tbody");
        $tbody.empty();
        if (data && data.list && data.list.length > 0) {
            var list = data.list;
            for (var i = 0; i < list.length; i++) {
                var l = list[i];
                var tr = "<tr data-id='" + l.id + "'>" +
                    "   <td>" + l.id + "</td>" +
                    "   <td class='art_title'>" + formatCont(l.art_title, 10) + "</td>" +
                    "   <td class='hidden-xs'>" + l.blog_navbar_name + "</td>" +
                    "   <td class='comm_user'>" + l.nickname + "</td>" +
                    "   <td class='comm_cont hidden'>" + l.content + "</td>" +
                    "   <td class='create_time hidden-xs'>" + l.create_time + "</td>" +
                    "   <td class='email hidden-xs hidden-sm'>" + l.email + "</td>" +
                    "   <td class='ip hidden-xs'>" + l.ip + "</td>" +
                    "   <td class='messageStatus'>" + formatStatus(l.audit_status) + "</td>" +
                    "   <td class='hidden-xs'>" + fmtEmpty(l.audit_time) + "</td>" +
                    "   <td class='hidden-xs'>" + fmtEmpty(l.auditor) + "</td>" +
                    "   <td class='table-operate' data-status='" + l.audit_status + "'>" +
                    "       <span class='col-lg-4 col-md-4 col-sm-12 col-xs-12'>" +
                    "          <a class='btn btn-danger opt-del dropdown-toggle opt-audit'>" +
                    "                   审核<i class='fa fa-sort-desc fa-lg'></i>" +
                    "          </a>" +
                    "           <ul class='dropdown-menu' role='menu'>" +
                    "             <li role='presentation' >" +
                    "                <a role='menuitem' tabindex='-1' data-status='1'>通过</a>" +
                    "             </li>" +
                    "             <li role='presentation'>" +
                    "                <a role='menuitem' tabindex='-1' data-status='-1'>不通过</a>" +
                    "             </li>" +
                    "          </ul>" +
                    "       </span>" +
                    "       <span class='col-lg-4 col-md-4 col-sm-12 col-xs-12'><a class='btn btn-warning opts opt-reply'><i class='fa fa-edit fa-md'></i> 回复 </a></span>" +
                    "       <span class='col-lg-4 col-md-4 col-sm-12 col-xs-12'><a class='btn btn-primary opts opt-detail'><i class='fa fa-navicon fa-md'></i> 查看 </a></span>" +
                    "   </td>" +
                    "</tr>";
                var $tr = $(tr).data("content", l.content);
                $tbody.append($tr)
            }
        }

        global_Art_Var.totalPage = data.pages;
        if (isLoadPagination || global_Art_Var.isLoadPagination) {
            global_Art_Var.isLoadPagination = false;
            $(".table-page ul.pagination li").not(".page-prev,.page-next").remove();
            var lis = "";
            for (var i = 0; i < data.pages; i++) {
                if (i == 0) {
                    lis += " <li class='active'><a href='javascript:void(0)'>" + (i + 1) + "</a></li>";
                } else {
                    lis += " <li><a href='javascript:void(0)'>" + (i + 1) + "</a></li>";
                }
            }
            if (global_Art_Var.totalPage === 1) {
                $(".table-page ul.pagination li.page-next").addClass("disabled");
                $(".table-page ul.pagination li.page-next").addClass("disabled");
            }
            $(".table-page ul.pagination li.page-prev").after(lis);
        }
    }).fail(function () {
        layer.msg('可能由于网络等原因加载失败！', function () {
        });
    }).always(function () {
        $(".content_loading").hide();
    });
    ;
}

function auditBtnClick() {
    var $this = $(this);
    var curStatus = $this.parents("td.table-operate").attr("data-status");
    var status = $this.attr("data-status");
    var id = $this.parents("tr").attr("data-id");
    if (!/\d/.test(status) || !/\d/.test(id)) {
        layer.msg("参数错误！");
        return false;
    }
    if (curStatus == status) {
        layer.msg("更新状态成功!");
        $this.parents("ul.dropdown-menu").slideUp();
        return false;
    }

    var text = "";
    if (status == -1) {
        text = "拒绝通过";
    } else if (status == 1) {
        text = "审核通过";
    }
    layer.confirm('您确定要' + text + '这条评论吗？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: "../message/back/auditMessage.zb",
            type: "POST",
            dataType: "JSON",
            data: {status: status, commentId: id, isSendEmail: 0}//不发送邮件
        }).done(function (data) {
            if (data && data.success) {
                layer.msg(data.msg);
                var $small = $(".sidebar-menu .comment small")
                $small.text(($small.text() - 0) - 1);
                if ($("#message-table tbody tr").length <= 1) {
                    global_Art_Var.isLoadPagination = true;
                }
                loadMessageList()
            } else {
                layer.msg(data.msg || "操作失败！", function () {
                });
            }
        }).fail(function () {
            layer.msg('可能由于网路原因操作失败！', function () {
            });
        }).always(function () {
            $this.parents("ul.dropdown-menu").slideUp();
        })
    });
}

function formatStatus(status) {
    var desc = "";
    switch (status) {
        case -1 :
            desc = "不通过";
            break;
        case 0 :
            desc = "待审核";
            break;
        case 1 :
            desc = "通过";
            break;
        default :
            desc = ""
    }
    return desc;
}
